package com.androidviewbadge

import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.view.ViewGroup
import android.widget.RadioGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.androidviewbadge.adapter2.AppAdapter
import com.viewbadge.BGABadgeFloatingActionButton
import com.viewbadge.BGABadgeFrameLayout
import com.viewbadge.BGABadgeImageView
import com.viewbadge.BGABadgeLinearLayout
import com.viewbadge.BGABadgeRadioButton
import com.viewbadge.BGABadgeRelativeLayout
import com.viewbadge.BGABadgeTextView
import com.viewbadge.BGABadgeView

class MainActivity : AppCompatActivity() {
    private lateinit var  mTestBv: BGABadgeView

    private lateinit var mTestBtv: BGABadgeTextView

    private lateinit var mNormalBiv: BGABadgeImageView
    private lateinit var mRoundedBiv: BGABadgeImageView
    private lateinit var mCircleBiv: BGABadgeImageView

    private lateinit var mTestBll: BGABadgeLinearLayout
    private lateinit var mTestBrl: BGABadgeRelativeLayout
    private lateinit var mTestBfl: BGABadgeFrameLayout

    private lateinit var mTabRg: RadioGroup
    private lateinit var mHomeBrb: BGABadgeRadioButton
    private lateinit var mMessageBrb: BGABadgeRadioButton
    private lateinit var mDiscoverBrb: BGABadgeRadioButton
    private lateinit var mMeBrb: BGABadgeRadioButton

    private lateinit var mChatBfab: BGABadgeFloatingActionButton
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main_layout)
        initView()
        testBadgeView()
        testRadioButton()
        testRecyclerView()
    }
  
    private fun initView() {
        mTestBv = findViewById(R.id.bv_main_test)
        mTestBtv = findViewById(R.id.btv_main_test)
        mNormalBiv = findViewById(R.id.biv_main_normal)
        mRoundedBiv = findViewById(R.id.biv_main_rounded)
        mCircleBiv = findViewById(R.id.biv_main_circle)
        mTestBll = findViewById(R.id.bll_main_test)
        mTestBrl = findViewById(R.id.brl_main_test)
        mTestBfl = findViewById(R.id.bfl_main_test)
        mTabRg = findViewById<RadioGroup>(R.id.rg_main_tab)
        mHomeBrb = findViewById(R.id.brb_main_home)
        mMessageBrb = findViewById(R.id.brb_main_message)
        mDiscoverBrb = findViewById(R.id.brb_main_discover)
        mMeBrb = findViewById(R.id.brb_main_me)
        mChatBfab = findViewById(R.id.bfab_main_chat)
    }
    private fun testBadgeView() {
        mTestBv.showTextBadge("9")
        mTestBv.badgeViewHelper.setBadgeTextSizeSp(15)
        mTestBv.badgeViewHelper.setBadgePaddingDp(8)
        mTestBv.badgeViewHelper.setBadgeTextColorInt(Color.parseColor("#FF0000"))
        mTestBv.badgeViewHelper.setBadgeBgColorInt(Color.parseColor("#00FF00"))
        mTestBv.badgeViewHelper.isDraggable = true
        mTestBv.badgeViewHelper.setBadgePaddingDp(7)
        mTestBv.badgeViewHelper.setBadgeBorderWidthDp(2)
        mTestBv.badgeViewHelper.setBadgeBorderColorInt(Color.parseColor("#0000FF"))
        mTestBtv.showCirclePointBadge()
        mNormalBiv.showCirclePointBadge()
        val avatarBadgeBitmap = BitmapFactory.decodeResource(resources, R.mipmap.badge_avatar_vip)
        val roundedDrawable = RoundedBitmapDrawableFactory.create(
                resources,
                BitmapFactory.decodeResource(resources, R.mipmap.badge_avator)
        )
        roundedDrawable.paint.isAntiAlias = true
        roundedDrawable.cornerRadius = 30f
        mRoundedBiv.setImageDrawable(roundedDrawable)
        mRoundedBiv.showDrawableBadge(avatarBadgeBitmap)
        val avatarBitmap = BitmapFactory.decodeResource(resources, R.mipmap.badge_avator)
        val circleDrawable = RoundedBitmapDrawableFactory.create(resources, avatarBitmap)
        circleDrawable.paint.isAntiAlias = true
        circleDrawable.cornerRadius = Math.max(avatarBitmap.width, avatarBitmap.height) / 2.0f
        mCircleBiv.setImageDrawable(circleDrawable)
        mCircleBiv.showDrawableBadge(avatarBadgeBitmap)
        mTestBll.showDrawableBadge(avatarBadgeBitmap)
        mTestBrl.showTextBadge("LoveAndroid")
        mTestBfl.showTextBadge("8")
        mChatBfab.showTextBadge("8")
        mChatBfab.setDragDismissDelegate {
            showToast("清空聊天消息")
        }
        Handler().postDelayed({ mRoundedBiv.hiddenBadge() }, 3000)
        Handler().postDelayed({ mRoundedBiv.showCirclePointBadge() }, 6000)
        Handler().postDelayed({
                                  mRoundedBiv.showDrawableBadge(
                                          BitmapFactory.decodeResource(
                                                  resources,
                                                  R.mipmap.badge_avatar_vip
                                          )
                                  )
                              }, 9000)
    }

    private fun testRadioButton() {
        mHomeBrb.showTextBadge("10")
        mMessageBrb.showTextBadge("1")
        mDiscoverBrb.showTextBadge("...")
        mMeBrb.showDrawableBadge(BitmapFactory.decodeResource(resources, R.mipmap.badge_avatar_vip))
        Handler().postDelayed({ mHomeBrb.showTextBadge("1") }, 5000)
        mHomeBrb.setDragDismissDelegate { showToast("消息单选按钮徽章拖动消失") }
        mTabRg.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.brb_main_home -> showToast("首页")
                R.id.brb_main_message -> showToast("消息")
                R.id.brb_main_discover -> showToast("发现")
                R.id.brb_main_me -> showToast("我")
            }
        }
    }
    fun testRecyclerView(){
        val recyclerview = findViewById<RecyclerView>(R.id.recyclerview)
        val adapter =object : AppAdapter<MessageModel>(this){
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
                return object :ViewHolder(R.layout.badge_item_message){
                    override fun onBindView(position: Int) {
                        val item = getItem(position)
                        findViewById<TextView>(R.id.tv_item_message_title).setText(item.title)
                        findViewById<TextView>(R.id.tv_item_message_detail).setText(item.detail)
                        val rootView = getItemView() as BGABadgeLinearLayout
                        if (item.newMsgCount > 0) {
                            rootView.showTextBadge("" + item.newMsgCount)
                            rootView.setDragDismissDelegate {
                                item.newMsgCount = 0
                                showToast(item.title + "的徽章消失")
                            }
                        } else {
                            rootView.hiddenBadge()
                        }
                    }

                }
            }
        }
        recyclerview.layoutManager = LinearLayoutManager(this)
        adapter.setOnItemClickListener { _, _, position ->
            showToast("点击了条目 " + adapter.getItem(position).title)
        }
        recyclerview.adapter = adapter
        adapter.addData(MessageModel.testData)
    }
    fun showToast(message:String?){
        AppToast.show(this,message)
    }
}

